import { fetchedItem } from "./EngineContext"
import Market from "./Market"
import Data from "./object/Data"
import funcUtil from "./utils/func.util"

export default class MarketContext {
  market: Market
  fetchedData: fetchedItem[]
  register: Market["register"]
  unregister: Market["unregister"]
  tryGetData: Market["tryGetData"]
  getModuleNameFromData: Market["getModuleNameFromData"]
  constructor(e: Market) {
    this.fetchedData = []
    this.market = e
    this.register = e.register.bind(e)
    this.unregister = e.unregister.bind(e)
    this.tryGetData = e.tryGetData.bind(e)
    this.getModuleNameFromData = e.getModuleNameFromData.bind(e)
  }
  async waitForData<A extends typeof Data = typeof Data>(t: A) {
    const startAwait = Date.now()
    const data = await this.market.waitForData(t)
    const finishAwait = Date.now()
    this.fetchedData.push({
      moduleName: this.getModuleNameFromData(funcUtil.getConstructor(data)),
      isModule: !1,
      startAwait,
      finishAwait
    })
    return data
  }
  getLoadedDependencies() {
    return this.fetchedData
  }
}
